package com.android.mmchat.xmlparser;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.android.mmchat.bean.AutoLoginBean;
import com.android.mmchat.bean.BaseBean;
import com.android.mmchat.common.SysParams;
import com.android.mmchat.common.EtouchException.NetworkException;
import com.android.mmchat.manager.NetManagerApache;
import com.android.mmchat.storage.Store;

import android.content.Context;
import android.os.Handler;

public class AutoLoginParser extends DataToObject{	
	Context c;	
	public AutoLoginParser(Context c) {
		super(c);
		this.c = c;
		// TODO Auto-generated constructor stub
	}

	@Override
	public BaseBean getMsgDBandNetwork(Handler handler, String interfaceType,
			Hashtable<String, String> table) throws NetworkException, ParserConfigurationException,
			SAXException, IOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BaseBean getMsgFromCacheDB(Handler handler, String interfaceType,
			Hashtable<String, String> table) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public AutoLoginBean getMsgFromNetwork(Handler handler, String interfaceType,
			Hashtable<String, String> table) throws Exception {
		// TODO Auto-generated method stub
		NetManagerApache mynetmanager = NetManagerApache.getInstance(c);
		SAXParserFactory spf = SAXParserFactory.newInstance();
		InputStream inStream = mynetmanager.doGetAsInputstream(SysParams.BASE_URL,table);
		SAXParser parser = spf.newSAXParser();
		AutoLoginHandler cch = implXmlHander();
		parser.parse(inStream, cch);
		return cch.getResult();
	}

	@Override
	public AutoLoginHandler implXmlHander() {
		// TODO Auto-generated method stub
		return new AutoLoginHandler(c);
	}
	class AutoLoginHandler extends XmlHandlerBase{
		private Context ctx;
		public AutoLoginHandler(Context c) {
			super(c);
			this.ctx = c;
			// TODO Auto-generated constructor stub
		}
		private AutoLoginBean ab = new AutoLoginBean();
		private boolean isStatus = false,isUid = false,isAuthkey = false,isIcon = false,isNickName=false ;

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			// TODO Auto-generated method stub
			if (localName.equals("status")) {
				isStatus = true;
			}else if (localName.equals("uid")){
				isUid=true;
			}else if(localName.equals("authkey")){
				isAuthkey = true;
			}else if(localName.equals("icon")){
				isIcon = true;
			}else if(localName.equals("nickName")){
				isNickName = true;
			}
			
		}
		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			// TODO Auto-generated method stub
			if (localName.equals("status")) {
				isStatus = false;
			}else if (localName.equals("uid")){
				isUid=false;
			}else if(localName.equals("authkey")){
				isAuthkey = false;
			}else if(localName.equals("icon")){
				isIcon =false;
			}else if(localName.equals("nickName")){
				isNickName =false;
			}
		}
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			// TODO Auto-generated method stub
			if (isStatus) {
				ab.resultStatus=new String(ch, start, length);
			}else if(isUid){
				ab.uid = new String(ch, start, length);
			}else if(isAuthkey){
				ab.authKey = new String(ch, start, length);
			}else if(isIcon){
				ab.icon=new String(ch, start, length);
			}else if(isNickName){
				ab.nickName=new String(ch, start, length);
			}
		}
		@Override
		public AutoLoginBean getResult() {
			// TODO Auto-generated method stub
			return this.ab;
		}
		@Override
		public boolean storeDataBean(BaseBean dataObject) {
			// TODO Auto-generated method stub
			return Store.getInstance(ctx).store2CacheDB(ab);
		}
	}
}
